package reduce.fz.com.reduce.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;

public class FindTeamFragment extends BaseListFragment<FindTeamFragment.TeamViewHolder,Team> {
    private Context context;
    private LayoutInflater layoutInflater;


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.context=view.getContext();
        this.layoutInflater=LayoutInflater.from(context);
    }


    public interface  TeamCardListener{

        public void onClickCard(Team team);
    }

    @Override
    public TeamViewHolder createItemViewHolder(ViewGroup parent) {
        View view=layoutInflater.inflate(R.layout.team_card_view,parent,false);
        TeamViewHolder holder=new TeamViewHolder(view, new TeamCardListener() {
            @Override
            public void onClickCard(Team team) {
                Intent intent=new Intent(getActivity(),null);
                intent.putExtra("teamid",team.teamid);
                startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public TeamViewHolder createfootViewHolder(View view) {
        return new TeamViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<Team> datas, Pagination pagination) {
        //翻页需要的startnum需要在这里面设置
        if(datas!=null&&!datas.isEmpty())
        pagination.setStartNum(datas.get(datas.size() - 1).categoryid);
        String url= ApiUtil.getApi(context,R.array.api_team_list);
        return url;
    }


    @Override
    protected int getPageType() {
        //返回startnum分页类型
        return Pagination.TYPE_START_NUM;
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回普通列表
        return null;
    }

    public static class TeamViewHolder extends BaseRecycleViewHolder<Team>{
        private TextView mTeamTitle;
        private TextView mTeamJoinDesc;
        private TeamCardListener teamCardListener;
        private View itemView;


        public TeamViewHolder(View itemView,TeamCardListener teamCardListener)
        {
            this(itemView);
            this.teamCardListener=teamCardListener;
        }


        public TeamViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            mTeamTitle= (TextView) itemView.findViewById(R.id.tv_title);
            mTeamJoinDesc=(TextView)itemView.findViewById(R.id.team_join_desc);
        }
        @Override
        public void renderView(final Team data, int position) {
            mTeamTitle.setText(data.title);
            mTeamJoinDesc.setText("共有"+data.teamAttach.joinnum+"参与中");
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    teamCardListener.onClickCard(data);
                }
            });


        }
    }


}
